package com.cloud.pole.shop.controller.order;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.feign.IExpressService;
import com.cloud.pole.shop.model.entity.order.ShopOrder;
import com.cloud.pole.shop.service.order.OrderService;

import lombok.extern.slf4j.Slf4j;

@Controller
@Slf4j
@RequestMapping("/express/front")
public class ExpressController {

	@Autowired
	private OrderService orderService;
	
	@Autowired
	private IExpressService iExpressService;
	
	/*
	 * 全国快递物流查询
	 */
	@ResponseBody
	@RequestMapping("/kdi")
    public ApiRestResponse<Object> kdi(
    		@RequestHeader(defaultValue = "") String token, 
    		@RequestParam(defaultValue = "") String orderSn){   	
    	try {
    		ParamException.err(StringUtils.isBlank(token), "token不能为空");    	
    		ParamException.err(StringUtils.isBlank(orderSn), "orderSn不能为空");    	
    		ShopOrder order = orderService.selectById(token, orderSn);
    		if(!order.getOrderStatus().equals(4) && !order.getOrderStatus().equals(10)){
    			return ApiRestResponse.fail("订单状态错误", null);
    		}
    		ApiRestResponse<String> result = iExpressService.kdi(order.getLogisticsInt(), null);
    		JSONObject jsonObj = JSONObject.parseObject(result.getData());
    		jsonObj.put("deliSddr", order.getDeliAddr());
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, jsonObj);
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }

}
